package com.ruoyi.wx.service.imp;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.constant.ConstantWxBase;
import com.ruoyi.system.domain.GoodsChoice;
import com.ruoyi.system.domain.WxUser;
import com.ruoyi.system.service.GoodsChoiceService;
import com.ruoyi.system.service.WxUserService;
import com.ruoyi.wx.pojo.vo.CommonMsgVo;
import com.ruoyi.wx.pojo.vo.MassMess;
import com.ruoyi.wx.pojo.vo.SendCommonTemplateVo;
import com.ruoyi.wx.pojo.vo.SendTemplateVo;
import com.ruoyi.wx.service.WxAuthService;
import com.ruoyi.wx.service.WxMessageService;
import com.ruoyi.wx.utils.WxMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxMessageServiceImp implements WxMessageService {

    @Autowired
    private WxAuthService wxAuthService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private GoodsChoiceService goodsChoiceService;

    /**
     * 获取模板消息 Id
     */
    @Override
    public void getTemplateId() {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("template_id_short", "");
        String result = HttpUtil.post(String.format(ConstantWxBase.Url.GET_TEMPLATE_ID.getUrl(), wxAuthService.adminGetAccessToken()), stringObjectHashMap);
        Map<String, Object> resultMap = JSON.parseObject(result, new TypeReference<Map<String, Object>>() {
        }.getType());
    }

    @Override
    public boolean sendTemplateMessage(SendTemplateVo sendTemplateVo) {
        String url = String.format(ConstantWxBase.Url.SEND_TEMPLATE_MESSAGE.getUrl(), wxAuthService.adminGetAccessToken());
        String result = HttpUtil
                .createPost(url)
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(sendTemplateVo)).execute().body();
        System.out.println(result);
        return true;
    }

    @Override
    public boolean sendCommonTemplateMessage(SendCommonTemplateVo sendTemplateVo) {
        String url = String.format(ConstantWxBase.Url.SEND_TEMPLATE_MESSAGE.getUrl(), wxAuthService.adminGetAccessToken());
        String result = HttpUtil
                .createPost(url)
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(sendTemplateVo)).execute().body();
        System.out.println(result);
        return true;
    }

    @Override
    public boolean massSendTemplateMessage(MassMess massMess) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("touser", massMess.getTouser());
        stringObjectHashMap.put("msgtype", massMess.getMsgtype());
        stringObjectHashMap.put("text", massMess.getText());
        String result = HttpUtil.createPost(String.format(ConstantWxBase.Url.MASS_SEND_TEMPLATE_MESSAGE.getUrl(), wxAuthService.adminGetAccessToken()))
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(stringObjectHashMap)).execute().body();
        Map<String, Object> resultMap = JSON.parseObject(result, new TypeReference<Map<String, Object>>() {
        }.getType());

        String errcode = resultMap.get("errcode").toString();
        System.out.println(errcode);
        return "0".equalsIgnoreCase(errcode);
    }

    @Override
    public void sendCommonMessage(CommonMsgVo commonMsgVo) {
        String result = HttpUtil.createPost(String.format(ConstantWxBase.Url.SEND_COMMON_MSG.getUrl(), wxAuthService.adminGetAccessToken()))
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(commonMsgVo)).execute().body();
        System.out.println(result);
    }

    @Override
    public void tipBusiness(String memberId, String content) {
        WxUser wxUser = wxUserService.queryById(Integer.parseInt(memberId));
        SendTemplateVo templateMessage = WxMessageUtils.getTemplateMessage(wxUser.getOpenId().trim(), content);
        this.sendTemplateMessage(templateMessage);
    }

    @Override
    public boolean c(int id, String userId, String content, String title, String status) {
        WxUser wxUser = wxUserService.queryById(Integer.parseInt(userId));
        SendCommonTemplateVo templateMessage = WxMessageUtils.getCommonTemplateMessage(wxUser.getOpenId().trim(), content, title, status);
        GoodsChoice goodsChoice = goodsChoiceService.queryById(id);
        goodsChoice.setSendMsgCount(goodsChoice.getSendMsgCount() + 1);
        goodsChoiceService.update(goodsChoice);
        return this.sendCommonTemplateMessage(templateMessage);
    }
}
